package com.mstar.qs.common.bean;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.morningstar.qs.api.basic.util.DatetimeUtil;

public class CommonCompositeMarket {

	private final Map<Integer, CommonExchange> exchanges = new HashMap<Integer, CommonExchange>();
	private final Map<Integer, CommonSecurity> securities = new HashMap<Integer, CommonSecurity>();

	public static CommonCompositeMarket buildInstance(
			Map<Integer, CommonExchange> exchs,
			Map<Integer, CommonSecurity> secus, List<CommonMarket> marks) {
		CommonCompositeMarket ccm = new CommonCompositeMarket();
		ccm.initExchange(exchs);
		ccm.initMarket(marks);
		ccm.initSecutiry(secus);
		return ccm;
	}

	public CommonMarket obtainMarket(int exchId, int secuType) {
		if (exchanges.containsKey(exchId))
			return exchanges.get(exchId).obtainMarket(secuType);
		return null;
	}

	private void initMarket(List<CommonMarket> marks) {
		for (CommonMarket cm : marks) {
			CommonExchange ce = exchanges.get(cm.getExchId());
			if (ce != null)
				ce.addMarkets(cm);
		}
	}

	private void initSecutiry(Map<Integer, CommonSecurity> secus) {
		for (Map.Entry<Integer, CommonSecurity> en : secus.entrySet()) {
			securities.put(en.getKey(), en.getValue());
		}
	}

	private void initExchange(Map<Integer, CommonExchange> exchs) {
		for (Map.Entry<Integer, CommonExchange> en : exchs.entrySet()) {
			exchanges.put(en.getKey(), en.getValue());
		}
	}


	public Map<Integer, CommonExchange> getExchanges() {
		return exchanges;
	}

	public Map<Integer, CommonSecurity> getSecurities() {
		return securities;
	}

	public CommonExchange obtainExchange(int exchId) {
		return exchanges.get(exchId);
	}

	public String obtainTimezoneId(int exchId) {
		CommonExchange ce = obtainExchange(exchId);
		if (ce != null)
			return ce.getTimezoneId();
		return null;
	}

	public int minutesOfDay(int exchId) {
		CommonExchange e = this.obtainExchange(exchId);
		if (e != null && e.getTimezoneId() != null) {
			return DatetimeUtil.minutesOfDay(e.getTimezoneId());
		}
		return -1;
	}

	public Integer marketOpenTime(int exchId, int secType) {
		CommonMarket cm = obtainMarket(exchId, secType);
		if (cm != null)
			return cm.getOpenMinute();
		return null;
	}

	public Integer marketOpenTimeWithDerived(int exchId, int secType) {
		CommonMarket cm = obtainMarket(exchId, secType);
		if (cm == null)
			return null;
		Integer open = cm.getOpenMinute();
		if (open > 10)
			return open;
		if (cm.getDerivedMarkId() > 0) {
			cm = obtainMarket(cm.getDerivedMarkId(), cm.getDerivedSecuType());
			if (cm != null) {
				open = cm.getOpenMinute();
			}
		}
		return open;
	}

	public Integer marketCloseTime(int exchId, int secType) {
		CommonMarket cm = obtainMarket(exchId, secType);
		if (cm != null)
			return cm.getCloseMinute();
		return null;
	}
	
	public Map<String, String> mapOfExchAndShortname() {
		Map<String, String> name=new HashMap<String, String>();
		Iterator<Entry<Integer, CommonExchange>> it=exchanges.entrySet().iterator();
		while(it.hasNext()) {
			Entry<Integer, CommonExchange> entry=it.next();
			name.put(String.valueOf(entry.getKey()),entry.getValue().getShortName());
		}
		return name;
	}
	
	
	/**
	 * Get Map between miccode and short name
	 * @return
	 */
	public Map<String, String> mapOfMiccodeAndshortname() {
		Map<String, String> name=new HashMap<String, String>();
		Iterator<Entry<Integer, CommonExchange>> it=exchanges.entrySet().iterator();
		while(it.hasNext()) {
			Entry<Integer, CommonExchange> entry=it.next();
			CommonExchange ce=entry.getValue();
			String shortname = ce.getShortName()==null ? "" : ce.getShortName();
			String micCodeAll = ce.getMicCode();
			if(micCodeAll==null) continue;
			String micCodes[] = micCodeAll.split(",");
			for(String micCode : micCodes){
				name.put(micCode, shortname);
			}
		}
		
		return name;
	}
	
	/**
	 * Get fixed map between exchange id and short name or miccode
	 * @return
	 */
	public Map<String, String> mapOfExchAndShortnameMiccode() {
		Map<String, String> name=new HashMap<String, String>();
		Iterator<Entry<Integer, CommonExchange>> it=exchanges.entrySet().iterator();
		while(it.hasNext()) {
			Entry<Integer, CommonExchange> entry=it.next();
			String n=entry.getValue().getShortName();
			if(n==null) n=entry.getValue().getMicCode();
			name.put(String.valueOf(entry.getKey()),n);
		}
		return name;
	}
	
/*	
	public String getExchangeMarketTime(){
		Map<String>
		List<ExcShortName> list = dao.findAll();
		JSONObject returnObj = new JSONObject();
		if(list!=null && list.size()>0){
			try {
				for(ExcShortName esn : list){
					JSONArray jaTime = new JSONArray();
					jaTime.put(esn.getOpeningTime());
					jaTime.put(esn.getClosingTime());
					returnObj.put(String.valueOf(esn.getExcId()), jaTime);
				}
			} 	catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return returnObj.toString();
	}*/
}
